/*
 * MIT License
 *
 * Copyright (c) 2020 dybai
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int hex_check(const char *buf)
{
    int i = 0;

    for (i = 0; buf[i]; i++)
    {
        if (((buf[i] >= '0') && (buf[i] <= '9'))
            || ((buf[i] >= 'a') && (buf[i] <= 'f'))
            || ((buf[i] >= 'A') && (buf[i] <= 'F')))
        {
            continue;
        }
        return 1;
    }
    return 0;
}

int hex_conv_to_num(const char *str, int *val)
{
    int ret = 0;
    int need_free = 0;
    int pos = 0;
    int bit = 0;
    int value = 0;
    char *buf = NULL;
    size_t len_str = 0;
    size_t len_buf = 0;

    len_buf = len_str = strlen(str);
    if (len_str % 2 != 0)
    {
        len_buf = len_str + 1;
        buf = malloc(len_buf);
        if (!buf)
        {
            ret = ENOMEM;
            goto e_alloc;
        }
        need_free = 1;
        memset(buf, 0, len_buf);
        strncpy(buf+1, str, len_str);
    }
    else
    {
        buf = (char *) str;
    }
    
    for (bit = 0, pos = len_buf-1; pos >= 0; bit += 4, pos--)
    {
        if ((buf[pos] >= '0') && (buf[pos] <= '9'))
        {
            value |= (buf[pos] - '0') << bit;
        }
        else if ((buf[pos] >= 'a') && (buf[pos] <= 'f'))
        {
            value |= (buf[pos] - 'a' + 0x0A) << bit;
        }
        else if ((buf[pos] >= 'A') && (buf[pos] <= 'F'))
        {
            value |= (buf[pos] - 'A' + 0x0A) << bit;
        }
    }

    *val = value;

    if (need_free) free(buf);
e_alloc:
    return ret;
}

#if 0
int main (int argc, char *argv[])
{
    int ret = 0;
    int value = 0;

    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s <HEX>\n", argv[0]);
        return 1;
    }

    if (hex_check(argv[1]))
    {
        fprintf(stderr, "ERROR: Illegal hexadecimal value: %s\n", argv[1]);
        return 1;
    }

    if ((ret = hex_conv_to_num(argv[1], &value)))
    {
        fprintf(stderr, "ERROR: Call conv_to_num() failed: %s\n", strerror(ret));
        return 1;
    }

    printf("%x", value);

    return 0;
}
#endif
